using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;

namespace VSTalk.Engine.Core.EnvironmentManagers.UI
{
    public class HostedControls : IHostedControls, IDisposable
    {
        private IEnumerable<IControlBuilder> _builders;

        public HostedControls(IEnumerable<IControlBuilder> builders)
        {
            _builders = builders;
        }

        public UserControl GetControl<TContext>()
        {
            return All.FirstOrDefault(control => control.DataContext is TContext);
        }

        public TContext GetContext<TContext>()
        {
            return All.Select(control => control.DataContext).OfType<TContext>().FirstOrDefault();
        }

        public IEnumerable<UserControl> All
        {
            get
            {
                foreach (var builder in _builders)
                {
                    yield return builder.Create();
                }
            }
        }

        public void Dispose()
        {
            
        }
    }
}